package cn.mothin.frame.util;

import java.util.List;

public class StringUtil {
    /**
     * 字符串去除全角半角空格
     * 
     * @param data 被处理的字符串
     * @return
     */
    public static String trim(String data) {
        if (data == null) {
            return null;
        }
        return data.trim().replace(" ", "").replace("　", "");
    }

    /**
     * 检查字符串是否是空字符串或为null
     * 
     * @param data 被检查的字符串
     * @return
     */
    public static boolean isNullOrEmpty(String data) {
        return data == null || trim(data).length() == 0;
    }

    /**
     * 对模糊查询特殊字符转义
     * 
     * @return
     */
    public static String escapeForFuzzyQuery(String param) {
        if (isNullOrEmpty(param)) {
            return param;
        }

        return param.replaceAll("/", "//").replaceAll("%", "/%")
                .replaceAll("_", "/_").replaceAll("％", "/％")
                .replaceAll("＿", "/＿");
    }

    /**
     * 对模糊查询特殊字符转义
     * 
     * @return
     */
    public static String makeFuzzyQuery(String param) {
        if (isNullOrEmpty(param)) {
            return param;
        }
        String s = escapeForFuzzyQuery(param);
        return "%" + s + "%";
    }

    /**
     * 字符串列表转字符串，用"，"间隔
     * @param list
     * @return
     */
    public static String arrayToString(List<String> list) {
        if (list == null || list.size() == 0) {
            return "";
        }
        String value = list.get(0);
        for (int i = 1; i < list.size(); i++) {
            value += "," + list.get(i);
        }
        return value;
    }
}
